﻿#region DaKeptLog
/*----------------------------------------------------------------
// 文件名：DaKeptLog.cs
// 功能描述：表dbo.Kept_Log的数据访问层
//
// 
// 创建时间：2009-11-20 DtataAccess template . Ver 5.0.20090413
//
// 修改标识： 
// 修改描述： 
//----------------------------------------------------------------*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Globalization;
using System.Text;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

using EQT.Model;


namespace EQT.Dal
{
    ///可以继承更上层接口,IKeptLog ,数据访问层基类DaBase，
    ///DaBase中包含了DbException异常处理静态方法
    public partial class DaKeptLog : DaBase<MoKeptLog>
    {
        #region SQL const
        internal const string TABLE_COLUMNS = " KPT_LOG_ID ,EQUIP_ID ,KPT_BEGIN ,USER_CODE1 ,USER_CODE2 ,KPT_END ,KPT_ADDR ,KPT_STATUS ,REM   ";

        internal const string SQL_INSERT = "INSERT INTO KEPT_LOG (" + TABLE_COLUMNS + ") VALUES (@KptLogId,@EquipId,@KptBegin,@UserCode1,@UserCode2,@KptEnd,@KptAddr,@KptStatus,@Rem)";
        internal const string SQL_SELECT = "SELECT " + TABLE_COLUMNS + " FROM KEPT_LOG  ";
        internal const string SQL_SELECT_ONE = SQL_SELECT + " WHERE KPT_LOG_ID=@KptLogId";

        internal const string SQL_EXIST = "SELECT COUNT(*) FROM KEPT_LOG  WHERE KPT_LOG_ID=@KptLogId  ";
        internal const string SQL_UPDATE = "UPDATE KEPT_LOG SET EQUIP_ID=@EquipId, KPT_BEGIN=@KptBegin, USER_CODE1=@UserCode1, USER_CODE2=@UserCode2, KPT_END=@KptEnd, KPT_ADDR=@KptAddr, KPT_STATUS=@KptStatus, REM=@Rem WHERE KPT_LOG_ID=@KptLogId";

        internal const string SQL_DELETE_DEFAULT = "DELETE FROM KEPT_LOG ";
        internal const string SQL_DELETE = "DELETE FROM KEPT_LOG WHERE KPT_LOG_ID=@KptLogId";

        internal const string SQL_COUNT = "SELECT COUNT(*) FROM Kept_Log ";
        #endregion

        #region Constructor

        /// <summary>
        /// 默认构造函数，使用配置文件中默认的数据库配置。
        /// </summary>
        public DaKeptLog()
        {
            this.db = DatabaseFactory.CreateDatabase();
        }
        /// <summary>
        /// 使用指定的databaseName数据库配置。
        /// </summary>
        /// <param name="databaseName">配置文件中数据库的配置名称。</param>
        public DaKeptLog(string databaseName)
        {
            this.db = DatabaseFactory.CreateDatabase(databaseName);
        }

        public DaKeptLog(Database db)
        {
            this.db = db;
        }

        #endregion

        //以下是CRUD方法，每个方法都有引入外部事务的版本

        #region 构造器模块

        internal override MoKeptLog ConstructT()
        {
            return new MoKeptLog();
        }
        #endregion



        #region Add模块 Helper

        ///build the command object.It never throw exception.
        internal override DbCommand ConstructAddCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);

            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "KptBegin", DbType.DateTime);
            db.AddInParameter(dbCommand, "UserCode1", DbType.AnsiString);
            db.AddInParameter(dbCommand, "UserCode2", DbType.AnsiString);
            db.AddInParameter(dbCommand, "KptEnd", DbType.DateTime);
            db.AddInParameter(dbCommand, "KptAddr", DbType.String);
            db.AddInParameter(dbCommand, "KptStatus", DbType.Int32);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            return dbCommand;
        }
        internal override DbCommand ConstructAddCommand(Database db, MoKeptLog entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_INSERT);
            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32, entity.KptLogId);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);
            db.AddInParameter(dbCommand, "KptBegin", DbType.DateTime, entity.KptBegin);
            db.AddInParameter(dbCommand, "UserCode1", DbType.AnsiString, entity.UserCode1);
            db.AddInParameter(dbCommand, "UserCode2", DbType.AnsiString, entity.UserCode2);
            db.AddInParameter(dbCommand, "KptEnd", DbType.DateTime, entity.KptEnd);
            db.AddInParameter(dbCommand, "KptAddr", DbType.String, entity.KptAddr);
            db.AddInParameter(dbCommand, "KptStatus", DbType.Int32, entity.KptStatus);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            return dbCommand;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, MoKeptLog entity)
        {
            db.SetParameterValue(addCmd, "KptLogId", entity.KptLogId);
            db.SetParameterValue(addCmd, "EquipId", entity.EquipId);
            db.SetParameterValue(addCmd, "KptBegin", entity.KptBegin);
            db.SetParameterValue(addCmd, "UserCode1", entity.UserCode1);
            db.SetParameterValue(addCmd, "UserCode2", entity.UserCode2);
            db.SetParameterValue(addCmd, "KptEnd", entity.KptEnd);
            db.SetParameterValue(addCmd, "KptAddr", entity.KptAddr);
            db.SetParameterValue(addCmd, "KptStatus", entity.KptStatus);
            db.SetParameterValue(addCmd, "Rem", entity.Rem);
            return addCmd;
        }

        internal override DbCommand PopulateAddCommandParameters(Database db, DbCommand addCmd, DataRow row)
        {
            db.SetParameterValue(addCmd, "KptLogId", row["KptLogId"]);
            db.SetParameterValue(addCmd, "EquipId", row["EquipId"]);
            db.SetParameterValue(addCmd, "KptBegin", row["KptBegin"]);
            db.SetParameterValue(addCmd, "UserCode1", row["UserCode1"]);
            db.SetParameterValue(addCmd, "UserCode2", row["UserCode2"]);
            db.SetParameterValue(addCmd, "KptEnd", row["KptEnd"]);
            db.SetParameterValue(addCmd, "KptAddr", row["KptAddr"]);
            db.SetParameterValue(addCmd, "KptStatus", row["KptStatus"]);
            db.SetParameterValue(addCmd, "Rem", row["Rem"]);
            return addCmd;
        }

        #endregion

        #region Update模块 Helper

        internal override DbCommand ConstructUpdateCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString);
            db.AddInParameter(dbCommand, "KptBegin", DbType.DateTime);
            db.AddInParameter(dbCommand, "UserCode1", DbType.AnsiString);
            db.AddInParameter(dbCommand, "UserCode2", DbType.AnsiString);
            db.AddInParameter(dbCommand, "KptEnd", DbType.DateTime);
            db.AddInParameter(dbCommand, "KptAddr", DbType.String);
            db.AddInParameter(dbCommand, "KptStatus", DbType.Int32);
            db.AddInParameter(dbCommand, "Rem", DbType.String);
            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32);

            return dbCommand;
        }
        internal override DbCommand ConstructUpdateCommand(Database db, MoKeptLog entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_UPDATE);
            db.AddInParameter(dbCommand, "EquipId", DbType.AnsiString, entity.EquipId);
            db.AddInParameter(dbCommand, "KptBegin", DbType.DateTime, entity.KptBegin);
            db.AddInParameter(dbCommand, "UserCode1", DbType.AnsiString, entity.UserCode1);
            db.AddInParameter(dbCommand, "UserCode2", DbType.AnsiString, entity.UserCode2);
            db.AddInParameter(dbCommand, "KptEnd", DbType.DateTime, entity.KptEnd);
            db.AddInParameter(dbCommand, "KptAddr", DbType.String, entity.KptAddr);
            db.AddInParameter(dbCommand, "KptStatus", DbType.Int32, entity.KptStatus);
            db.AddInParameter(dbCommand, "Rem", DbType.String, entity.Rem);
            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32, entity.KptLogId);

            return dbCommand;
        }

        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, MoKeptLog entity)
        {
            db.SetParameterValue(updateCmd, "EquipId", entity.EquipId);
            db.SetParameterValue(updateCmd, "KptBegin", entity.KptBegin);
            db.SetParameterValue(updateCmd, "UserCode1", entity.UserCode1);
            db.SetParameterValue(updateCmd, "UserCode2", entity.UserCode2);
            db.SetParameterValue(updateCmd, "KptEnd", entity.KptEnd);
            db.SetParameterValue(updateCmd, "KptAddr", entity.KptAddr);
            db.SetParameterValue(updateCmd, "KptStatus", entity.KptStatus);
            db.SetParameterValue(updateCmd, "Rem", entity.Rem);
            db.SetParameterValue(updateCmd, "KptLogId", entity.KptLogId);
            return updateCmd;
        }
        internal override DbCommand PopulateUpdateCommandParameters(Database db, DbCommand updateCmd, DataRow row)
        {
            db.SetParameterValue(updateCmd, "EquipId", row["EquipId"]);
            db.SetParameterValue(updateCmd, "KptBegin", row["KptBegin"]);
            db.SetParameterValue(updateCmd, "UserCode1", row["UserCode1"]);
            db.SetParameterValue(updateCmd, "UserCode2", row["UserCode2"]);
            db.SetParameterValue(updateCmd, "KptEnd", row["KptEnd"]);
            db.SetParameterValue(updateCmd, "KptAddr", row["KptAddr"]);
            db.SetParameterValue(updateCmd, "KptStatus", row["KptStatus"]);
            db.SetParameterValue(updateCmd, "Rem", row["Rem"]);
            db.SetParameterValue(updateCmd, "KptLogId", row["KptLogId"]);
            return updateCmd;
        }

        internal override void PrepareDataAdapterCommand(Database db, out DbCommand dbInsertCommand,
                             out DbCommand dbUpdateCommand, out DbCommand dbDeleteCommand)
        {
            dbInsertCommand = db.GetSqlStringCommand(SQL_INSERT);
            #region set insert cmd parameters
            db.AddInParameter(dbInsertCommand, "KptLogId", DbType.Int32, "KPT_LOG_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "EquipId", DbType.AnsiString, "EQUIP_ID", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "KptBegin", DbType.DateTime, "KPT_BEGIN", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "UserCode1", DbType.AnsiString, "USER_CODE1", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "UserCode2", DbType.AnsiString, "USER_CODE2", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "KptEnd", DbType.DateTime, "KPT_END", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "KptAddr", DbType.String, "KPT_ADDR", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "KptStatus", DbType.Int32, "KPT_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbInsertCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            #endregion
            dbUpdateCommand = db.GetSqlStringCommand(SQL_UPDATE);
            #region Set update cmd value parameters
            db.AddInParameter(dbUpdateCommand, "EquipId", DbType.AnsiString, "EQUIP_ID", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "KptBegin", DbType.DateTime, "KPT_BEGIN", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "UserCode1", DbType.AnsiString, "USER_CODE1", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "UserCode2", DbType.AnsiString, "USER_CODE2", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "KptEnd", DbType.DateTime, "KPT_END", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "KptAddr", DbType.String, "KPT_ADDR", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "KptStatus", DbType.Int32, "KPT_STATUS", DataRowVersion.Current);
            db.AddInParameter(dbUpdateCommand, "Rem", DbType.String, "REM", DataRowVersion.Current);
            #endregion
            #region set update cmd pk where parameters
            db.AddInParameter(dbUpdateCommand, "KptLogId", DbType.Int32, "KPT_LOG_ID", DataRowVersion.Current);
            #endregion
            dbDeleteCommand = db.GetSqlStringCommand(SQL_DELETE);
            #region set delete cmd pk where parameters
            db.AddInParameter(dbDeleteCommand, "KptLogId", DbType.Int32, "KPT_LOG_ID", DataRowVersion.Current);
            #endregion
        }
        #endregion

        #region Delete模块 Helper

        internal override DbCommand ConstructDeleteCommand(Database db)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32);

            return dbCommand;
        }
        internal override DbCommand ConstructDeleteCommand(Database db, MoKeptLog entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_DELETE);

            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32, entity.KptLogId);

            return dbCommand;
        }

        internal override DbCommand ConstructDeleteCommandForWhere(Database db, string where)
        {
            return db.GetSqlStringCommand(SQL_DELETE_DEFAULT + where);
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, MoKeptLog entity)
        {
            db.SetParameterValue(deleteCmd, "KptLogId", entity.KptLogId);
            return deleteCmd;
        }

        internal override DbCommand PopulateDeleteCommandParameters(Database db, DbCommand deleteCmd, DataRow row)
        {
            db.SetParameterValue(deleteCmd, "KptLogId", row["KptLogId"]);
            return deleteCmd;
        }

        #endregion helper

        #region Query模块 Helper

        internal override DbCommand ConstructQueryCommand(string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region GetEntity(s)模块 Helper

        internal override DbCommand ConstructSelectOneCommand(Database db, MoKeptLog entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_SELECT_ONE);

            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32, entity.KptLogId);
            return dbCommand;
        }

        internal override void PopulateEntityByDataReader(IDataReader reader, ref MoKeptLog entity)
        {
            if (!reader.IsDBNull(0)) entity.KptLogId = reader.GetInt32(0);
            if (!reader.IsDBNull(1)) entity.EquipId = reader.GetString(1);
            if (!reader.IsDBNull(2)) entity.KptBegin = reader.GetDateTime(2);
            if (!reader.IsDBNull(3)) entity.UserCode1 = reader.GetString(3);
            if (!reader.IsDBNull(4)) entity.UserCode2 = reader.GetString(4);
            if (!reader.IsDBNull(5)) entity.KptEnd = reader.GetDateTime(5);
            if (!reader.IsDBNull(6)) entity.KptAddr = reader.GetString(6);
            if (!reader.IsDBNull(7)) entity.KptStatus = reader.GetInt32(7);
            if (!reader.IsDBNull(8)) entity.Rem = reader.GetString(8);
        }



        internal override DbCommand ConstructSelectConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_SELECT + condition);
        }

        #endregion

        #region Count模块 Helper

        internal override DbCommand ConstructCountCommand(Database db)
        {
            return db.GetSqlStringCommand(SQL_COUNT);
        }

        internal override DbCommand ConstructCountConditionCommand(Database db, string condition)
        {
            return db.GetSqlStringCommand(SQL_COUNT + condition);
        }

        #endregion

        #region IsExist模块 Helper

        internal override DbCommand ConstructIsExistCommand(Database db, MoKeptLog entity)
        {
            DbCommand dbCommand = db.GetSqlStringCommand(SQL_EXIST);
            db.AddInParameter(dbCommand, "KptLogId", DbType.Int32, entity.KptLogId);
            return dbCommand;
        }

        #endregion



    }
}
#endregion

